<template>
  <el-dialog
    ref="refDlg"
    title="异常审核"
    :visible.sync="dialogShow"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :before-close="clearForm"
    width="600px"
    @close="closeDialog"
  >
    <div>
      <el-form ref="refForm" class="financeForm" :rules="formRules" :model="formData" size="mini" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="审核结果:" prop="reviewResult">
              <el-select v-model="formData.reviewResult" placeholder="请选择审核结果" style="width:100%" @change="changeResult">
                <el-option
                  v-for="item in revienResultList"
                  :key="item.dictCode"
                  :label="item.dictName"
                  :value="item.dictCode"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item v-if="formData.exceptionTypeCode === 'EXCEPT007'" label="调整后时间:" prop="afterReviewTime">
              <el-date-picker
                v-model="formData.afterReviewTime"
                value-format="yyyy-MM-dd HH:mm:ss"
                align="right"
                type="datetime"
                placeholder="选择日期"
                :picker-options="pickerOptions"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="审核意见:" size="mini" prop="reviewAdvice">
              <el-input v-model="formData.reviewAdvice" type="textarea" placeholder="请输入审核意见" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div slot="footer">
      <el-button size="mini" @click="closeDialog">取消</el-button>
      <el-button size="mini" type="primary" @click="submitForm">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getDictByPatchType } from '@/api/dataDict'
import { getExceptionDeatil, examineException } from '@/api/storeConstruction'
export default {
  name: 'AddUnitOffice',
  props: {
    visible: {
      type: Boolean,
      default: false,
      required: true
    },
    detailData: {
      type: [String, Object],
      default: ''
    }
  },
  data() {
    return {
      pickerOptions: {
        disabledDate(time) {
          var date = new Date()
          var y = date.getFullYear()
          var m = date.getMonth() + 1
          var d = date.getDate()
          var nowTime = new Date(`${y}-${m}-${d} 00:00:00`)
          return time.getTime() < nowTime.getTime()
        }
      },
      formData: {
        afterReviewTime: '',
        reviewAdvice: '',
        reviewResult: ''
      },
      formRules: {
        reviewResult: [
          { required: true, message: '请选择审核结果' }
        ]
      },
      revienResultList: []
    }
  },

  computed: {
    dialogShow: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    }
  },
  created() {
    this.getDictByPatchTypeAll(['REVIEW_RESULT'])
      .then(res => {
        this.revienResultList = res.REVIEW_RESULT
      })
    getExceptionDeatil(this.detailData.id).then(res => {
      if (res.success && res.data) {
        this.formData = res.data
        this.$nextTick(() => {
          this.$refs.refForm.clearValidate()
        })
      }
    })
  },

  methods: {
    async getDictByPatchTypeAll(data) {
      const obj = await getDictByPatchType(data)
        .then(res => {
          return res
        })
      return obj
    },
    // 选择审核结果
    changeResult(value) {
      if (value === '1') {
        if (this.formData.exceptionTypeCode === 'EXCEPT007') {
          this.$set(this.formData, 'afterReviewTime', this.formData.shopExceptionDetailDtos[0].afterAdjustTime)
        }
      } else {
        this.$set(this.formData, 'afterReviewTime', '')
      }
    },
    // 关闭弹窗
    closeDialog() {
      this.dialogShow = false
      this.clearForm()
    },
    // 清空表单
    clearForm(done) {
      this.formData = {
        // afterReviewTime: '',
        // reviewAdvice: '',
        // reviewResult: ''
      }
      this.$refs.refForm && this.$refs.refForm.clearValidate()
      done && done()
    },
    // 提交表单处理
    submitForm() {
      if (this.submiting) return
      this.$refs['refForm'].validate((valid) => {
        if (valid) {
          this.submiting = true
          examineException(this.formData).then(res => {
            this.submiting = false
            if (res.success === 1) {
              this.$message({
                message: '操作成功',
                type: 'success'
              })
              this.clearForm()
              this.dialogShow = false
              const isNew = !this.formData.id // 是否新增数据 true (新增则刷新列表到第一页)/ false (非新增则刷新当前页)
              this.$emit('success', isNew)
            }
          }).catch(() => {
            this.submiting = false
          })
        }
      })
    }
  }
}
</script>

<style lang='scss' scoped>

</style>
